using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("Acms.WebControls.FlashControl.js", "application/x-javascript")]
namespace Acms.WebControls
{
    [ToolboxData("<{0}:FlashControl runat=server></{0}:FlashControl>")]
    public class FlashControl : WebControl
    {
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        private string flashFileUrl;
        public string FlashFileUrl
        {
            get { return flashFileUrl; }
            set { flashFileUrl = value; }
        }

        private string minVersion = "6.0.0.0";
        public string MinVersion
        {
            get { return minVersion; }
            set { minVersion = value; }
        }

        public enum WModes
        {
            Window,
            Opaque,
            Transparent
        }

        private WModes wMode = WModes.Window;
        public WModes WMode
        {
            get { return wMode; }
            set { wMode = value; }
        }

        public bool AllowFullScreen { get; set; }

        private Dictionary<string, string> variables;
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public Dictionary<string, string> Variables
        {
            get
            {
                if (variables == null)
                {
                    variables = new Dictionary<string, string>();
                }
                return variables;
            }
            set
            {
                variables = value;
            }
        }

        protected override object SaveViewState()
        {
            ViewState["flashFileUrl"] = flashFileUrl;
            ViewState["minVersion"] = minVersion;
            ViewState["wMode"] = wMode;
            ViewState["fullScreen"] = AllowFullScreen;
            ViewState["Variables"] = variables;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            flashFileUrl = ViewState["flashFileUrl"] as string;
            minVersion = ViewState["minVersion"] as string;
            if(ViewState["wMode"] is WModes)
                wMode = (WModes)ViewState["wMode"];
            variables = ViewState["Variables"] as Dictionary<string,string>;
            AllowFullScreen = (bool)ViewState["fullScreen"];
        }
	

        protected override void OnPreRender(EventArgs e)
        {
            if (!string.IsNullOrEmpty(FlashFileUrl))
            {
                Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Acms.WebControls.FlashControl.js");
                StringBuilder javascript = new StringBuilder();
                javascript.AppendFormat("var {0}_flashvars={{}};var {0}_params={{}};var {0}_atts={{}};", this.ClientID);
                //add in the wmode param
                javascript.AppendFormat("{0}_params.wmode = \"{1}\";", this.ClientID, this.WMode.ToString().ToLower());
                if (AllowFullScreen)
                {
                    //add in the full screen
                    javascript.AppendFormat("{0}_params.allowFullScreen = \"true\";", this.ClientID);
                }

                foreach (string key in Variables.Keys)
                {
                    javascript.AppendFormat("{0}_flashvars.{1} = \"{2}\";", this.ClientID, key, Variables[key]);
                }

                javascript.AppendFormat("swfobject.embedSWF(\"{0}\", \"{1}\", \"{2}\", \"{3}\", \"{4}\", false, {1}_flashvars, {1}_params, {1}_atts);", Page.ResolveUrl(FlashFileUrl), this.ClientID, this.Width.Value, this.Height.Value, this.MinVersion);
                Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, javascript.ToString(), true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), this.ClientID, javascript.ToString(), true);
            }
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(FlashFileUrl))
            {
                base.Render(writer);
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            RenderDefaultAlternateContent(writer);
        }

        private void RenderDefaultAlternateContent(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.P);

            writer.AddAttribute(HtmlTextWriterAttribute.Href, "http://www.adobe.com/go/getflashplayer");
            writer.RenderBeginTag(HtmlTextWriterTag.A);

            writer.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif");
            writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Get Adobe Flash player");
            writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);

            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
        }



    }
}
